package com.mxl.service.cashorder.rule;

import java.text.MessageFormat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mxl.common.MD5Util;
import com.mxl.common.StringUtil;
import com.mxl.common.helper.ResponseErrorCode;
import com.mxl.common.rule.AbstractRule;
import com.mxl.service.cashorder.dto.CashOrderRequest;

public class CheckSign extends AbstractRule {
	private static final Logger logger = LoggerFactory
			.getLogger(CheckSign.class);

	@Override
	protected boolean makeDecision(Object obj) throws Exception {

		CashOrderRequest c = (CashOrderRequest) obj;
		// 把签名字段 加签
		// 比对签名version=%smember_code=%sorder_time=%sorder_id=%sorder_amount=%scard_no=%scard_pwd=%smerchant_key=%s
		String signO = MessageFormat
				.format("version={0}member_code={1}order_time={2}order_id={3}order_amount={4}card_no={5}card_pwd={6}merchant_key={7}",
						c.getVersion(), c.getMemberCode(), c.getOrderTime(),
						c.getOrderId(), c.getOrderAmount().toString(), c.getCardNo(),
						c.getCardPwd(), c.getCustomMerchant().get(0)
								.getMacKey());
		logger.debug("CheckSign berfor signO:" + signO);
		String md5Sign = new String(MD5Util.md5Hex(signO).toLowerCase());
		logger.debug("CheckSign after md5Sign:" + md5Sign);
		if (StringUtil.isEmpty(c.getSignMsg())
				|| !md5Sign.equals(c.getSignMsg())) {
			c.setMsgId(ResponseErrorCode.SIGN_ERROR.getValue());
			return false;
		}
		return true;
	}

}
